/**
 * 火焰狼带着他的部队
 * From: 芬芬时尚潮流
 */
var setupTask;

//设置多久开启一次(小时:为单位)
var starttime = 1;

//设置每次活动结束的时间(分钟:为单位)
var overtime = 30;

//设置每次副本开启的分钟段(分钟:为单位)
var startminute = 28;

var mapList = Array(100000000, 104000000, 102000000, 220000000, 240000000);
//开始地图
//var mapId = 100000000;
var mapId = mapList[Math.floor(Math.random() * mapList.length)];

//怪物列表
//var mobList = Array(100001, 100004, 100005, 100006, 100007, 8220022, 8220023, 8220024, 8220025, 8220026);
var mobList = Array(9480235, 9480236, 9480237, 9480238, 9480239, 8644409, 8644410, 8644411, 8644412);
//怪物的x坐标
var xPoint;
//怪物的y左边
var yPoint;
//boss的x坐标
var boosxPoint;
//boss的y左边
var boosyPoint;

//坐标
var xyPoint;
//地图名称
var mapname;
switch (mapId) {
case 100000000:
	mapname = "射手村";
	xPoint = Array(3199, 3010, 2682, 2400, 2002, 1709, 1421, 1205, 962, 754);
	yPoint = Array(334, 334, 334, 334, 334, 334, 334, 334, 315, 274);
	boosxPoint = 1874;
	boosyPoint = 334;
	break;
case 104000000:
	mapname = "明珠港";
	xPoint = Array(854, 1090, 1331, 1554, 1782, 2009, 2209, 734, 978, 2279);
	yPoint = Array(647, 647, 647, 647, 647, 647, 647, 647, 587, 587);
	boosxPoint = 1673;
	boosyPoint = 647;
	break;
case 102000000:
	mapname = "勇士部落";
	xPoint = Array(442, 656, 784, 928, 1071, 1207, 1408, 1567, 1742, 2090);
	yPoint = Array(1935, 1935, 1935, 1935, 1935, 1935, 1935, 1935, 1935, 1935);
	boosxPoint = 2518;
	boosyPoint = 1935;
	break;
case 220000000:
	mapname = "玩具城";
	xPoint = Array(-907, -733, -535, -358, -189, -13, 192, 407, 576, 804);
	yPoint = Array(102, 102, 102, 102, 102, 102, 102, 102, 102, 102);
	boosxPoint = -103;
	boosyPoint = 102;
	break;
case 240000000:
	mapname = "神木村";
	xPoint = Array(-210, 189, -389, -547, -742, 349, 518, 702, 852, -741);
	yPoint = Array(272, 272, 351, 422, 422, 325, 422, 422, 422, 422);
	boosxPoint = -31;
	boosyPoint = 272;
	break;
}

function init() {
	em.setProperty("state", "0");
	scheduleNew();
}

function scheduleNew() {
	var cal = java.util.Calendar.getInstance();
	var hour = cal.get(java.util.Calendar.HOUR_OF_DAY); //获得小时 24小时制
	var minute = cal.get(java.util.Calendar.MINUTE); //获得分钟
	var second = cal.get(java.util.Calendar.SECOND); //获得秒
	cal.set(java.util.Calendar.HOUR, 0); //设定的小时 12小时制
	cal.set(java.util.Calendar.MINUTE, startminute); //设定的分钟
	cal.set(java.util.Calendar.SECOND, 0); //设定的秒钟
	var nextTime = cal.getTimeInMillis();
	while (nextTime <= java.lang.System.currentTimeMillis()) {
		nextTime += 1000 * 60 * 60 * starttime; //设置多久开启
	}
	setupTask = em.scheduleAtTimestamp("startEvent", nextTime);
}

function startEvent() {
	var cal = java.util.Calendar.getInstance();
	var hour = cal.get(java.util.Calendar.HOUR_OF_DAY); //获得小时  24小时制
	var minute = cal.get(java.util.Calendar.MINUTE); //获得分钟
	var second = cal.get(java.util.Calendar.SECOND); //获得秒
	cal.set(java.util.Calendar.HOUR, 0); //设定的小时 12小时制
	cal.set(java.util.Calendar.MINUTE, startminute); //设定的分钟
	cal.set(java.util.Calendar.SECOND, 0); //设定的秒钟
	var nextTime = cal.getTimeInMillis();
	while (nextTime <= java.lang.System.currentTimeMillis()) {
		nextTime += 1000 * 60 * overtime; //设置多久结束
	}
	setupTask = em.scheduleAtTimestamp("finishEvent", nextTime);

	var eim = em.newInstance("MonsterAttackCity");
	var map = eim.setInstanceMap(mapId);
	map.resetFully();
	eim.getMapFactory().getMap(mapId).killAllMonsters(false);

	//先生成此次攻城的领导- 也就是BOSS
	var mob = em.getMonster(9101078);
	var overrideStats = em.newMonsterStats();
	var hprand = 10000000000; //设置100E血
	overrideStats.setOHp(hprand);
	overrideStats.setOExp(500000);
	overrideStats.setOMp(200000);
	mob.setOverrideStats(overrideStats);
	mob.setHp(hprand);
	eim.registerMonster(mob);
	map.spawnMonsterOnGroundBelow(mob, new java.awt.Point(boosxPoint, boosyPoint));
	overrideStats.setOHp(hprand);
	overrideStats.setOExp(500000);
	overrideStats.setOMp(200000);
	mob.setOverrideStats(overrideStats);
	mob.setHp(hprand);
	eim.registerMonster(mob);
	map.spawnMonsterOnGroundBelow(mob, new java.awt.Point(boosxPoint, boosyPoint));

	//召唤怪物
	spawnMonster(eim);

	em.broadcastServerMsg(5120129, "火焰狼带着他的大部队在[" + mapname + "]登录..拿起你的勇气来拯救部落.", true);
	em.broadcastServerMsg("[守卫村落]  怪物大军已经抵达到[" + mapname + "]拿起武器狠狠的收拾他们！");
}

//召唤怪物
function spawnMonster(eim) {
	var map = eim.getMapInstance(mapId);
	var overrideStats = em.newMonsterStats();
	for (var a = 0; a < 2; a++) {
		for (var i = 0; i < mobList.length; i++) {
			var mob = em.getMonster(mobList[i]);
			var hprand = 188888888;
			overrideStats.setOHp(hprand);
			overrideStats.setOExp(80000);
			overrideStats.setOMp(200000);
			mob.setOverrideStats(overrideStats);
			mob.setHp(hprand);
			eim.registerMonster(mob);
			map.spawnMonsterOnGroundBelow(mob, new java.awt.Point(xPoint[i], yPoint[i])); //刷出这个怪物
		}
	}
}

//杀死怪物触发
function monsterValue(eim, mobId) {
	if (em.getProperty("state") == 0) { //如果状态0的话在召唤
		if (eim.getMapInstance(mapId).getAllMonstersThreadsafe().size() == 10) { //如果地图上还有1个怪物的话就继续召唤
			java.lang.System.out.println("boss还没有挂.在召唤一波小怪"); 
			spawnMonster(eim);
		}
		if (mobId == 9101078) {
			em.setProperty("state", "1"); //击败BOSS 让状态变成1 让他不在循环召唤怪物
			em.broadcastServerMsg(5120129, "恭喜各位勇士们成功的击败火焰狼大军.", true);
			em.broadcastServerMsg("[守卫村落] 恭喜勇士们在[" + mapname + "]击退了火焰狼大军.");
			java.lang.System.out.println("人数" + eim.getPlayerCount()); 
			eim.getMapInstance(mapId).killAllMonsters(true); //BOSS死亡之后杀死地图上所有怪物
			java.lang.System.out.println("人数" + eim.getPlayerCount()); 
			return 0;
		}
	}
	return 1;
}

function finishEvent() {
	if (em.getProperty("state") == 0) {
		var eim = em.newInstance("MonsterAttackCity");
		var map = eim.getMapInstance(mapId);
		em.setProperty("state", "2"); //时间到
		map.killAllMonsters(true);

		em.broadcastServerMsg("[守卫村落] 经过奋斗.最终还是没能击败火焰狼和他的大军.火焰狼大军将会在.每小时的28分钟-58分钟分进攻城市，每隔" + starttime + "小时开放一次！每次进攻城市将会持续" + overtime + "分钟");
	}
	scheduleNew();
}

function cancelSchedule() {
	if (setupTask != null) {
		setupTask.cancel(true);
	}
	
function openNpc(eim, npcid, mode) {
	for (var i = 0; i < eim.getPlayerCount(); i++) {
		eim.getPlayers().get(i).openNpc(npcid, mode);
	}
}
}
